package com.starlight.flower_server.infrastructure.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.domain.article.cqrs.ArticlePageQuery;
import com.starlight.flower_server.infrastructure.entity.Article;
import com.starlight.flower_server.infrastructure.mapper.ArticleMapper;
import com.starlight.flower_server.infrastructure.repository.ArticleRepository;
import com.starlight.flower_server.infrastructure.util.CurrentUserInfoUtils;
import com.starlight.flower_server.infrastructure.util.PageUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * @Author yfy2001
 * @date 2023/4/13 14:38
 */
@Repository
public class ArticleRepositoryImpl implements ArticleRepository {

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public Article queryDetail(Long id){
        return articleMapper.selectById(id);
    }

    @Override
    public Integer logicDelete(Long id) {
        return articleMapper.deleteById(id);
    }

    @Override
    public Integer insert(Article article) {
        String operator= CurrentUserInfoUtils.get("username");
        article.setCreator(operator);
        article.setModifier(operator);
        article.setCreated(new Date());
        article.setModified(new Date());
        article.setIsDeleted(0);
        article.setHit(0);
        article.setCommentNum(0);
        return articleMapper.insert(article);
    }

    @Override
    public Integer modify(Article article) {
        String modifier=CurrentUserInfoUtils.get("username");
        article.setModifier(modifier);
        article.setModified(new Date());
        return articleMapper.updateById(article);
    }

    @Override
    public Integer addCommentNumByArticleId(Long articleId, Integer num) {
        Article article=articleMapper.selectById(articleId);
        article.setCommentNum(article.getCommentNum()+num);
        return articleMapper.updateById(article);
    }

    @Override
    public Integer addHitByArticleId(Long articleId) {
        Article article=articleMapper.selectById(articleId);
        article.setHit(article.getHit()+1);
        return articleMapper.updateById(article);
    }

    @Override
    public IPage<Article> queryPage(ArticlePageQuery query) {
        QueryWrapper<Article> queryWrapper= PageUtils.createOrderQuery(query);
        queryWrapper.lambda()
                .isNotNull(Article::getId)
                .eq(Objects.nonNull(query.getId()), Article::getId,query.getId())
                .like(Objects.nonNull(query.getTitle()),Article::getTitle,query.getTitle())
                .like(Objects.nonNull(query.getFlowerUserId()),Article::getFlowerUserId,query.getFlowerUserId());
        return articleMapper.selectPage(PageUtils.startPage(query),queryWrapper);
    }

    @Override
    public List<Article> queryAll() {
        return articleMapper.selectList(new LambdaQueryWrapper<Article>()
                .isNotNull(Article::getId));
    }

    @Override
    public Long queryTotalCount() {
        return articleMapper.selectCount(new LambdaQueryWrapper<Article>()
                .isNotNull(Article::getId));
    }

    @Override
    public List<Article> queryRandom(Integer count) {
        return articleMapper.selectList(new LambdaQueryWrapper<Article>()
                .isNotNull(Article::getId)
                .last(String.format("order by rand() limit %d",count)));
    }


}
